package com.hk.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.hk.model.News;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.service.INewsService;
import com.hk.service.impl.NewsServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: zhe
 * @Date: 2021/6/17 9:47
 * @Statement: 查询所有新闻和添加新闻
 */
@WebServlet("/news/NewsQueryOrSave")
public class NewsQueryOrSaveServlet extends HttpServlet {

    INewsService iNewsService = new NewsServiceImpl();

    /**
     * 查询所有新闻信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        String page = Tool.nullToString(req.getParameter("page"));
        String limit = Tool.nullToString(req.getParameter("limit"));
        String title = Tool.nullToString(req.getParameter("title"));
        String releaseDate = Tool.nullToString(req.getParameter("releaseDate"));
        String typeId = Tool.nullToString(req.getParameter("typeId"));
        int i = Tool.parseInt(page);
        int size = Tool.parseInt(limit);
        int intTypeId = Tool.parseInt(typeId);
//        封装对象
        News news = new News();
        news.setTitle(title);
        news.setReleaseDate(releaseDate);
        news.setTypeId(intTypeId);
//        System.out.println("news="+news);
//        根据sql语句规则将每页页码修改为需要数值
        int pageNum = Tool.pageNum(i,size);
        List<News> newsList = iNewsService.findAll(pageNum,size,news);
        int count = iNewsService.count(-1,-1, news);
        PrintWriter out = resp.getWriter();
        if (newsList.isEmpty()) {
            ResultVO<News> resultVO = new ResultVO<>(0,"error",0,newsList);
            String newsListJSON = JSONObject.toJSONString(resultVO);
            out.println(newsListJSON);
        } else {
            ResultVO<News> resultVO = new ResultVO<>(0,"success",count,newsList);
            String newsListJSON = JSONObject.toJSONString(resultVO);
            out.println(newsListJSON);
        }
        out.flush();
        out.close();



    }

    /**
     * 添加新闻信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        News news = gson.fromJson(reader, News.class);
        PrintWriter out = resp.getWriter();
        Result result = new Result();
        if (news.getTypeId() < 1) {
            result.setStatus(0);
            result.setMessage("error");
            String resultJSON = JSONObject.toJSONString(result);
            out.println(resultJSON);
            out.flush();
            out.close();
        }
        int save = iNewsService.save(news);
        if (save < 1) {
            result.setStatus(save);
            result.setMessage("error");
        } else {
            result.setStatus(save);
            result.setMessage("success");
        }
        String resultJSON = JSONObject.toJSONString(result);
        out.println(resultJSON);
        out.flush();
        out.close();


    }
}
